package cn.com.chec.en.controller.front;

import cn.com.chec.en.dao.NewsInfoDao;
import cn.com.chec.en.dao.PageLinkDao;
import cn.com.chec.en.dao.TagInfoDao;
import cn.com.chec.en.model.domain.NewsInfo;
import cn.com.chec.en.model.domain.QueryNewsObject;
import cn.com.chec.en.model.domain.TagInfo;
import cn.com.chec.en.model.dto.NewsDTO;
import cn.com.chec.en.util.ContactUtils;
import cn.com.chec.en.util.PageBean;
import cn.com.chec.en.util.StringTools;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class NewsFrontController {
	protected static final Logger logger = Logger
			.getLogger(NewsFrontController.class);

	@Resource
	private PageLinkDao pageLinkDao;

	@Resource
	private NewsInfoDao newsInfoDao;

	@Resource
	private TagInfoDao tagInfoDao;

	@Resource
	private Map<String, Object> environment;

	@RequestMapping({ "/news" })
	public ModelAndView list(
			@RequestParam(value = "curPage", required = false, defaultValue = "1") String curPage,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("news");

		Map<String, String> pageLinkMap = this.pageLinkDao.getAllAsMap();
		logger.info("[NewsFrontController] pageLinkMap=" + pageLinkMap);
		map.put("pageLinkMap", pageLinkMap);

		map.putAll(ContactUtils.getContactMap());

		PageBean pageBean = new PageBean();
		int curPageNew = (StringUtils.isNotBlank(curPage))
				&& (curPage.matches("\\d+")) ? Integer.parseInt(curPage) : 1;
		pageBean.setCurPage(curPageNew);
		QueryNewsObject queryObject = new QueryNewsObject();
		queryObject.setLanguageId(1);
		queryObject.setIsShowInList(1);

		List<NewsDTO> newsInfoTopList = new ArrayList<NewsDTO>();
		List<NewsDTO> newsInfoNotTopList = new ArrayList<NewsDTO>();

		queryObject.setStart(pageBean.getStart());
		queryObject.setNum(pageBean.getRowsPerPage());
		queryObject.setIsTop(1);
		newsInfoTopList = this.newsInfoDao.getNewsListByQueryChannelId(
				queryObject, 1);

		queryObject.setIsTop(0);
		queryObject.setOrderBy(1);
		newsInfoNotTopList = this.newsInfoDao.getNewsListByQueryChannelId(
				queryObject, 1);
		pageBean.setMaxRowCount(this.newsInfoDao.getNewsCountByQueryChannelId(
				queryObject, 1));
		pageBean.setMaxPage();
		pageBean.setPageNoList();

		map.put("newsInfoTopList", newsInfoTopList);
		map.put("newsInfoNotTopList", newsInfoNotTopList);
		map.put("pageBean", pageBean);

		map.putAll(this.environment);
		mav.addAllObjects(map);

		return mav;
	}

	@RequestMapping(value = { "/news/{id}" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	public ModelAndView detail(@PathVariable String id,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("newsDetail");

		Map<String, String> pageLinkMap = this.pageLinkDao.getAllAsMap();
		logger.info("[NewsFrontController] pageLinkMap=" + pageLinkMap);
		map.put("pageLinkMap", pageLinkMap);

		map.putAll(ContactUtils.getContactMap());

		int newsId = (StringUtils.isNotBlank(id)) && (id.matches("\\d+")) ? Integer
				.valueOf(id).intValue() : 0;
		NewsInfo newsInfo = this.newsInfoDao.getNewsInfoByNewsId(newsId);

		StringBuilder tags = new StringBuilder();
		String tag = newsInfo.getTag();
		if (StringUtils.isNotBlank(tag)) {
			tags.append("<br/><div class=\"txt_tag\">");
			String[] tagIds = tag.split(",");
			for (String t : tagIds) {
				if (StringUtils.isNotBlank(t)) {
					TagInfo tagInfo = this.tagInfoDao.getTagById(Integer
							.valueOf(t).intValue());
					if (tagInfo == null) {
						continue;
					}
					tags.append("<a href=\"" + request.getContextPath()
							+ "/tag/" + tagInfo.getId() + ",\">"
							+ tagInfo.getTag() + "</a>&nbsp;");
				}
			}
			tags.append("</div>");
		}

		map.put("newsInfo", newsInfo);
		map.put("tags", tags);

		map.putAll(this.environment);
		mav.addAllObjects(map);

		return mav;
	}

	@RequestMapping({ "/news/search" })
	public ModelAndView search(
			@RequestParam(value = "kw", defaultValue = "", required = true) String kw,
			@RequestParam(value = "curPage", defaultValue = "1", required = false) String curPage,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("newsSearch");

		Map<String,String> pageLinkMap = this.pageLinkDao.getAllAsMap();
		logger.info("[NewsFrontController] pageLinkMap=" + pageLinkMap);
		map.put("pageLinkMap", pageLinkMap);

		map.putAll(ContactUtils.getContactMap());

		PageBean pageBean = new PageBean();
		int curPageNews = (StringUtils.isNotBlank(request
				.getParameter("curPage")))
				&& (request.getParameter("curPage").matches("\\d+")) ? Integer
				.parseInt(request.getParameter("curPage")) : 1;
		pageBean.setCurPage(curPageNews);
		kw = StringUtils.isNotBlank(request.getParameter("kw")) ? request
				.getParameter("kw") : "";
		kw = StringTools.converISO2UTF8(kw);
        kw = StringEscapeUtils.unescapeHtml(kw);
		QueryNewsObject queryObject = new QueryNewsObject();
		queryObject.setLanguageId(1);
		queryObject.setKeywords(kw);
		queryObject.setStart(pageBean.getStart());
		queryObject.setNum(pageBean.getRowsPerPage());
		List<NewsDTO> newsInfoList = this.newsInfoDao
				.getNewsListByQueryPChannelIdDistinct(queryObject, 0);
		pageBean.setMaxRowCount(this.newsInfoDao
				.getNewsCountByQueryPChannelIdDistinct(queryObject, 0));
		pageBean.setMaxPage();
		pageBean.setPageNoList();

		map.put("newsInfoDTOList", newsInfoList);
		map.put("kw", StringEscapeUtils.escapeHtml(kw));
		map.put("pageBean", pageBean);

		map.putAll(this.environment);
		mav.addAllObjects(map);

		return mav;
	}
}